<?php if (!defined('BASEPATH')) exit('No direct script access allowed');

class Sendmail extends CI_Controller
{
	function __construct()
	{
		parent::__construct();

		$this->load->helper('url');
	//	$this->load->library('tank_auth');
	}

	function index()
	{
		//设置邮件发送协议，这里使用smtp
		$config['protocol']     = 'smtp';
		//设置我们使用的smtp_host地址
		$config['smtp_host']    = 'smtp.139.com';
		//设置我们的邮箱地址
		$config['smtp_user']    = '13850860619@139.com';
		//设置邮箱密码
		$config['smtp_pass']    = 'fifstone';
		//设置端口号
		$config['smtp_port']    = '25';
		//设置默认字符集
		$config['charset']      = 'utf-8';
		//设置邮件类型，可选html或text
		$config['mailtype']     = 'text';
		//设置smtp超时设置(秒)
		$config['smtp_timeout'] = '5';
		//设置换行符
		$config['newline'] = "\r\n";
		//以上$config配置信息也可放在config/email.php 文件内，ci能自动使用
		
		
		$mail_text='';
		$this->load->database();
		$sql=('SELECT id, title, start_date AS start,allday FROM task WHERE start_date <= DATE_ADD( NOW(),INTERVAL 1 DAY ) AND start_date >= DATE_SUB( NOW() , INTERVAL 1 DAY ) ');		
		$query = $this->db->query($sql);
		if ($query->num_rows() > 0)
		{
			foreach ($query->result_array() as $row)
			{
				$mail_text.= sprintf("任务%s %s (%s) \n\r",$row['id'],$row['title'],$row['start']);
			}
		}
//		echo $mail_text;
		if($mail_text){
			//载入CI的email类
			$this->load->library ('email', $config);
			//设置发信人的email地址及名字
			$this->email->from ('13850860619@139.com', 'thinktodo.cn');
			//设置收信人的地址以及名字
			$this->email->to ('597361@qq.com', 'stone5');
			//设置邮件的主题
			$this->email->subject ('您'.date("Y/m/d").'的thinktodo日程');
			//设置邮件的内容
			$this->email->message ($mail_text);
			//发送邮件
			$this->email->send ();
			 
			//为了查看是否工作，可以使用如下进行Debug
			echo $this->email->print_debugger();
		
		}
		
	}	
		
		
		
		
		
		
		
		
		
		
		
		
		
		

/* 			$mail = new SaeMail();
			$mail->clean(); //重用此对象
			//$ret = $mail->quickSend("597361@qq.com", "邮件标题", "邮件内容", "64619166@qq.com", "kfrdwrzdaetxcahe", "smtp.qq.com", 25); //指定smtp和端口
			$ret = $mail->quickSend("597361@qq.com", '您'.date("Y/m/d").'的thinktodo日程', $mail_text, "13850860619@139.com", "fifstone", "smtp.139.com", 25); //指定smtp和端口
			//发送失败时输出错误码和错误信息
			if ($ret === false) {
				var_dump($mail->errno(), $mail->errmsg());
			}	 */			
		

	
    
    function welcome(){
        if (!$this->tank_auth->is_logged_in()) {
			redirect('/auth/login/');
		} else {
			$data['user_id']	= $this->tank_auth->get_user_id();
			$data['username']	= $this->tank_auth->get_username();
			$this->load->view('welcome', $data);
		}
        
    }
	
	function sendmail(){

		$mail = new SaeMail();

		// $mail->setAttach(array("my_photo" => "照片的二进制数据"));
		// $ret = $mail->quickSend("to@sina.cn", "邮件标题", "邮件内容", "smtpaccount@gmail.com", "password");

		//发送失败时输出错误码和错误信息
		// if ($ret === false) {
			// var_dump($mail->errno(), $mail->errmsg());
		// }

		$mail->clean(); //重用此对象
		//$ret = $mail->quickSend("597361@qq.com", "邮件标题", "邮件内容", "64619166@qq.com", "kfrdwrzdaetxcahe", "smtp.qq.com", 25); //指定smtp和端口
		$ret = $mail->quickSend("597361@qq.com", "邮件标题ss", "邮件内容ss", "13850860619@139.com", "fifstone", "smtp.139.com", 25); //指定smtp和端口

		//发送失败时输出错误码和错误信息
		if ($ret === false) {
			var_dump($mail->errno(), $mail->errmsg());
		}
		
	}
	
}

